package controllers

import (
	"book/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
)

type BookController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// 展示
func (c *BookController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	name := c.GetString("name")
	where := map[string]string{}
	if name != "" {
		where["name"] = name
	}
	key := "book-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		models.DB.Model(&models.Book{}).Count(&count)
		var book []models.Book
		offset := (page - 1) * size
		err = models.DB.Offset(offset).Limit(size).Where(where).Find(&book).Error
		pageData["count"] = count
		pageData["book"] = book
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 500,
				Msg:  "数据查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, time.Second*10)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "mysql数据查询成功",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "redis数据查询成功",
			Data: pageData,
		}
		c.ServeJSON()
		return
	}
}

// 添加
func (c *BookController) Add() {
	name := c.GetString("name")
	zz := c.GetString("zz")
	img := c.GetString("img")
	states := c.GetString("states")
	address := c.GetString("address")
	if name == "" {
		fmt.Println("书名不能为空")
		return
	}
	book := models.Book{
		Name:    name,
		Zz:      zz,
		Img:     img,
		States:  states,
		Address: address,
	}
	err := models.DB.Create(&book).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "数据添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "数据添加成功",
			Data: book,
		}
		c.ServeJSON()
	}
}

// 删除
func (c *BookController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Where("ID = ?", id).Delete(&models.Book{}, id).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "数据删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "数据删除成功",
			Data: id,
		}
		c.ServeJSON()
	}
}

// 修改
func (c *BookController) Upt() {

}

// 文件上传
func (c *BookController) Uoload() {
	file, hesder, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "请上传文件",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + hesder.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgs := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "文件上传成功",
			Data: imgs,
		}
		c.ServeJSON()
	}
}
